package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.PatientCategory;
import com.itheima.service.PatientCategoryService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.MediaTypeNotSupportedStatusException;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/patientCategory")
public class PatientCategoryController {

   @Reference
   private PatientCategoryService patientCategoryService;

   @RequestMapping("/findPage")
   public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
      PageResult pageResult = patientCategoryService.findPage(queryPageBean);
      return pageResult;
   }

   /**
    * 通过会员id查询会员详细信息,包括订单信息,评估结果
    * @param id
    * @return
    */
   @RequestMapping("/findById/{id}")
   public Result findById(@PathVariable Integer id) {
      try {
         Member member = patientCategoryService.findById(id);
         return new Result(true, MessageConstant.QUERY_PATIENTCATEGORY_SUCCESS, member);
      } catch (Exception e) {
         e.printStackTrace();
         return new Result(false, MessageConstant.QUERY_PATIENTCATEGORY_FAIL);
      }
   }


   @RequestMapping("/findAll")
   public Result findAll() {
      try {
         String[] categorys = patientCategoryService.findAll();
         return new Result(true, MessageConstant.QUERY_PATIENTCATEGORY_SUCCESS, categorys);
      } catch (Exception e) {
         e.printStackTrace();
         return new Result(false, MessageConstant.QUERY_PATIENTCATEGORY_FAIL);
      }
   }

   /**
    * 通过会员id查询对应的分类名称
    * @return
    */
   @RequestMapping("/findCategoryNamesByMemberId/{memberId}")
   public Result findCategoryNamesByMemberId(@PathVariable Integer memberId) {
      try {
         String[] categoryNames = patientCategoryService.findCategoryNamesByMemberId(memberId);
         return new Result(true, MessageConstant.QUERY_PATIENTCATEGORY_SUCCESS, categoryNames);
      } catch (Exception e) {
         e.printStackTrace();
         return new Result(false, MessageConstant.QUERY_PATIENTCATEGORY_FAIL);
      }
   }

   @RequestMapping("/setCategory")
   public Result setCategory(@RequestBody Map map) {
      try {
         patientCategoryService.setCategory(map);
      } catch (Exception e) {
         e.printStackTrace();
         return new Result(false, MessageConstant.EDIT_CATEGORY_FAIL);
      }
      return new Result(true, MessageConstant.EDIT_CATEGORY_SUCCESS);
   }
}
